package com.xiaoyu.client.ui.activity.help;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.discovery.DiscoveryRankAdapter;
import com.xiaoyu.client.ui.fragment.rank.RankingFragment;
import com.xiaoyu.commonlib.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 互动榜单
 */
public class RankingActivity extends BaseActivity implements View.OnClickListener{
    public static final String RANKING_TYPE = "ranking_type";
    @BindView(R.id.activity_ranking_title_back_btn)
    ImageView backBtn;//返回按钮
    @BindView(R.id.activity_ranking_all_name_layout)
    RadioGroup rankGroup;//榜单title group
    @BindView(R.id.activity_ranking_content_layout)
    FrameLayout mContentLayout;//内容碎片容器

    private List<Fragment> mList = new ArrayList<>();
    private String[] rankName = new String[]{"day","week","month"};
    private int type;//榜单类别 0：日榜 、1：周榜 、2：月榜

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);
        ButterKnife.bind(this);
        parseIntent();
        initView();
    }

    //接收从其他界面传过来的值
    private void parseIntent() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            type = bundle.getInt(DiscoveryRankAdapter.RANK_TYPE);
        }
    }

    //初始化控件
    private void initView() {
        if (type == 0){
            ((RadioButton)rankGroup.getChildAt(0)).setChecked(true);
        }else if (type == 1){
            ((RadioButton)rankGroup.getChildAt(1)).setChecked(true);
        }else if (type == 2){
            ((RadioButton)rankGroup.getChildAt(2)).setChecked(true);
        }

        mList.clear();
        for (int i=0;i<rankGroup.getChildCount();i++){
            RankingFragment fragment = new RankingFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(RANKING_TYPE,i);
            fragment.setArguments(bundle);
            mList.add(i,fragment);
        }
        showFragment(type);

        backBtn.setOnClickListener(this);
        rankGroup.setOnCheckedChangeListener(checkedChangeListener);

    }

    /**
     * 显示第一个fragment
     * @param index
     */
    private void showFragment(int index){
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction mTransaction = mFragmentManager.beginTransaction();
        mTransaction.replace(R.id.activity_ranking_content_layout,mList.get(index));
        mTransaction.commit();
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
    }

    private RadioGroup.OnCheckedChangeListener checkedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.activity_ranking_day_name://日榜
                    showFragment(0);
                    break;
                case R.id.activity_ranking_week_name://周榜
                    showFragment(1);
                    break;
                case R.id.activity_ranking_month_name://月榜
                    showFragment(2);
                    break;
            }
        }
    };
}
